﻿using System;
using System.Windows.Media.Imaging;
using System.Windows.Media;

namespace DotNetImage.Imaging.Filters
{
    public class SkewFilter<TPixel, TColorChannel> : FilterBase
    {
        public SkewFilter() { HasMultiThreadingSupport = true; RequiresInternalRefresh = true; }

        /// <summary>
        /// Gets or sets the x-axis skew angle, which is measured in degrees counterclockwise from the y-axis.
        /// The default is 0.
        /// </summary>
        [ParameterAttribute(Name = "AngleX", DefaultValue = 0, MaxValue = Int32.MaxValue, MinValue = 0)]
        public double AngleX { get; set; }

        /// <summary>
        /// Gets or sets the y-axis skew angle, which is measured in degrees counterclockwise from the x-axis.
        /// The default is 0.
        /// </summary>
        [ParameterAttribute(Name = "AngleY", DefaultValue = 0, MaxValue = Int32.MaxValue, MinValue = 0)]
        public double AngleY { get; set; }

        /// <summary>
        /// Gets or sets the x-coordinate of the transform center.
        /// The default is 0.
        /// </summary>
        [ParameterAttribute(Name = "CenterX", DefaultValue = 0, MaxValue = Int32.MaxValue, MinValue = 0, Step = 1)]
        public double CenterX { get; set; }

        /// <summary>
        /// Gets or sets the y-coordinate of the transform center.
        /// The default is 0.
        /// </summary>
        [ParameterAttribute(Name = "CenterY", DefaultValue = 0, MaxValue = Int32.MaxValue, MinValue = 0, Step = 1)]
        public double CenterY { get; set; }


        protected override void ProcessPerPixel(DotNetImage bmp)
        {
            TransformedBitmap tb = new TransformedBitmap();
            tb.BeginInit();
            tb.Source = bmp.BitmapSource;
            var transform = new SkewTransform();
            transform.AngleX = AngleX;
            transform.AngleY = AngleY;
            transform.CenterX = CenterX;
            transform.CenterY = CenterY;
            tb.Transform = transform;
            tb.EndInit();  // "Transform must be a combination of scales, flips, and 90 degree rotations"
        }
    }
}
